package com.hisense.ovcloud.apicontrol.tool;

import com.hisense.hitv.ByteUtils;
import com.hisense.hitv.EncryptDES;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Slf4j
public class EncryptUtils {
    private static final String Algorithm_DESEDE = "DESede";

    public EncryptUtils() {
    }

    public static String DESEncrypt(String key, String src, int srcEncLen) {
        byte[] temp = new byte[srcEncLen];
        byte[] srcArray = src.getBytes();
        System.arraycopy(srcArray, 0, temp, 0, srcArray.length < srcEncLen ? srcArray.length : srcEncLen);
        byte[] enc = EncryptDES.encrypt(key.getBytes(), temp);
        String outString = ByteUtils.byte2Str(enc, 16);
        return outString.toUpperCase();
    }

    public static String DESDecrypt(String key, String src) {
        try {
            byte[] enc = ByteUtils.string2Byte(src);
            byte[] dec = EncryptDES.decrypt(key.getBytes(), enc);
            String tmp = new String(dec, "UTF-8");
            int index = tmp.indexOf("\u0000");
            return tmp.substring(0, index);
        } catch (UnsupportedEncodingException var6) {
            return null;
        } catch (Exception var7) {
            return null;
        }
    }

    public static String hashEncrypt(String plainText, String algorithm) {
        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException var7) {
            NoSuchAlgorithmException e = var7;
            e.printStackTrace();
        }

        md.update(plainText.getBytes());
        byte[] b = md.digest();
        StringBuilder output = new StringBuilder(32);

        for(int i = 0; i < b.length; ++i) {
            String temp = Integer.toHexString(b[i] & 255);
            if (temp.length() < 2) {
                output.append("0");
            }

            output.append(temp);
        }

        return output.toString().toUpperCase();
    }

    public static String hashEncryptMulti(String plainText, String algorithm, int times) {
        for(int i = 0; i < times; ++i) {
            plainText = hashEncrypt(plainText, algorithm);
        }

        return plainText;
    }

    public static byte[] desedeEncrypt(byte[] keybyte, byte[] src) {
        try {
            SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
            Cipher c1 = Cipher.getInstance("DESede");
            c1.init(1, deskey);
            return c1.doFinal(src);
        } catch (NoSuchAlgorithmException var4) {
            NoSuchAlgorithmException e1 = var4;
            e1.printStackTrace();
        } catch (NoSuchPaddingException var5) {
            NoSuchPaddingException e2 = var5;
            e2.printStackTrace();
        } catch (Exception var6) {
            Exception e3 = var6;
            e3.printStackTrace();
        }

        return null;
    }

    public static byte[] md5Encrypt(String inputStr) {
        return DigestUtils.md5(inputStr);
    }

    public static byte[] rsaDecryptByPrivateKey(byte[] data, byte[] key) throws Exception {
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(2, privateKey);
        return cipher.doFinal(data);
    }

    public static byte[] rsaDecryptByPublicKey(byte[] data, byte[] key) throws Exception {
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(2, publicKey);
        return cipher.doFinal(data);
    }

    public static byte[] rsaEncryptByPublicKey(byte[] data, byte[] key) throws Exception {
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(1, publicKey);
        return cipher.doFinal(data);
    }

    public static byte[] rsaEncryptByPrivateKey(byte[] data, byte[] key) throws Exception {
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(1, privateKey);
        return cipher.doFinal(data);
    }

    public static PrivateKey getPrivateKey(byte[] privateKeyBytes) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
        return privateKey;
    }

    public static PublicKey getPublicKey(byte[] publicKeyBytes) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        return publicKey;
    }

    public static void main(String[] args) {
        String encSrc = "164001156";
        System.out.println("encSrc = " + encSrc);
        String enc = DESDecrypt("ecf8427e5d933e61", "C27B0C26A484E06D063E22B610C3B536");
        System.out.println(enc);
    }
}